/*
 * TophemanTimerManager
 *
 * @author Christophe Rosset <tophe@topheman.com>
 * http://blog.topheman.com/
 */

TophemanTimerManager = function(delay){

    //variables
    var timeCounter,delay;//@private

    this.timerId;//timerId managing setInterval

    //methodes

    /**
     * Starts the timer
     * @param function callback will be fired on .start() call @optional
     * @param int milliseconds specify at how milliseconds the counter will begin @optional
     *
     * If a timer is already rolling, it will be cleared without calling its callback
     */
    this.start = function(callback,milliseconds){
        console.info('--> Timer.start()');
        if(this.timerId){
            console.info("A timer is already rolling, it will be overrided");
            clearInterval(this.timerId);
        }

        timeCounter = new TophemanTimeCounter(milliseconds);
        this.timerId = setInterval(function(){
            timeCounter.addDelay(delay);
            //console.info(timeCounter.getTimeElapsed());
        },delay);
        if(callback && typeof callback == "function"){
            callback.apply(this,arguments);
        }
    }

    /**
     * Stops the timer and returns the time elapsed since .start() or .resume() was fired
     * @param function callback will be fired on .stop() call @optional
     * @return int milliseconds elapsed since started
     *
     * If no timer to stop, will return false
     */
    this.stop = function(callback){
        console.info('>-- Timer.stop()');
        if(!this.timerId){
            console.info("No timer to stop, callback won't be evaluated");
            return false;
        }
        clearInterval(this.timerId);
        delete(this.timerId);
        if(callback && typeof callback == "function"){
            callback.apply(this,arguments);
        }
        return this.getTimeElapsed();
    }

    /**
     * Restart the timer at the time it last stop when .stop() was fired
     * @param function callback will be fired on .resume() call @optional
     *
     * If the timer has not been started nor stopped, will restard timer at 0
     */
    this.resume = function(callback){
        console.info('--> Timer.resume()');
        var millisecondsStart;
        if(typeof timeCounter == 'undefined'){
            console.info("No timer to resume, will start timer at 0");
            millisecondsStart = 0;
        }
        else{
            millisecondsStart = this.getTimeElapsed();
        }
        this.start(callback,millisecondsStart);
    }

    /**
     * Public method to access timeElapsed on the private timeCounter object
     * @return int milliseconds elapsed since started
     */
    this.getTimeElapsed = function(){
        //console.info('myApp.helpers.timer.getTimeElapsed() - timeCounter',timeCounter);
        if(timeCounter){
            //console.info('timecounter ok',timeCounter.getTimeElapsed());
            return timeCounter.getTimeElapsed();
        }
        else{
            //console.info('timecounter ko',0);
            return 0;
        }
    }
    
    /**
     * Public method to set timeElapsed on the private timeCounter object
     * @return int milliseconds elapsed since started
     */
    this.setTimeElapsed = function(timeElapsed){
        if(timeCounter){
            timeCounter.setTimeElapsed(timeElapsed);
            //console.info('myApp.helpers.timer.setTimeElapsed('+timeElapsed+')');
        }
    }

    /**
     * Changes the delay (will only be effective on the next call to .start() )
     * @param int newDelay
     */
    this.setDelay = function(newDelay){
        delay = newDelay;
    }

    //debug à retirer
    this.getTimeStarted = function(){
        return timeCounter.started;
    }

    /**
     * Object TophemanTimeCounter used to store and manage the time elapsed (stays in private scope)
     * @param int timeElapsed specify at how milliseconds the counter will begin @optional
     */
    var TophemanTimeCounter = function(timeElapsed){

        this.started = timeElapsed;//debug

        //constructor
        if(typeof timeElapsed == 'undefined')
            timeElapsed = 0;
        timeElapsed = parseInt(timeElapsed);
        if(isNaN(timeElapsed))
            timeElapsed = 0;

        //methods
        this.addDelay = function(delay){
            timeElapsed += delay;
        }
        this.getTimeElapsed = function(){
            return timeElapsed;
        }
        this.setTimeElapsed = function(newTimeElapsed){
            timeElapsed = newTimeElapsed;
        }
        this.resetCounter = function(){
            timeElapsed = 0;
        }
    }

}